<?php
namespace Admin\Controller;

class DadminController extends BaseController {

    public function index() {

        $did = $_REQUEST['did'];
        $map = null;
        !empty($did) && $map['department_id'] = $did;

        $Dadmin = D ('Dadmin');

        $limit = \Api\Libs\PageUtil::getLimit(10);
        $page = \Api\Libs\PageUtil::getPageHtml($Dadmin->getCountByWhere($map),10);

        //P($limit);
        $Department = D ('Department');
        $departmentList = $Department->getAllDepartment();

        $dadminList = $Dadmin->getDadminByWhere($map,$limit);

        // 获取系别名称
        foreach ($dadminList as $k=>$v) {
            $dadminList[$k]['dname'] = $Department->getDepartmentName($v['department_id']);
        }

        $this->assign('dadminList',$dadminList);
        $this->assign('departmentList',$departmentList);
        $this->assign('page',$page);
        $this->assign('did',$did);
        $this->display();
    }



    public function add() {

        if (!IS_POST) {

            $Department = D ('Department');
            $departmentList = $Department->getAllDepartment();

            $this->assign('departmentList',$departmentList);
            $this->display();
        } else {

            $data['account'] = $_REQUEST['account'];
            $data['email'] = $_REQUEST['email'];
            $data['telphone'] = $_REQUEST['telphone'];
            $data['tname'] = $_REQUEST['tname'];
            $data['department_id'] = $_REQUEST['department_id'];

            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $data['password'] = md5('123456');

            $Dadmin = D ('Dadmin');
            $flag = $Dadmin->addDadmin($data);

            if ($flag === '000000')
                $this->out('RESULT_OK','',__APP__.'/Dadmin');
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }

    }


    public function edit() {

        if (!IS_POST) {

            $id = $_REQUEST['id'];
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $Dadmin = D ('Dadmin');
            $d = $Dadmin->getDadminById($id);
            if (empty($d)) $this->error('院系管理员不存在');

            $Department = D ('Department');
            $departmentList = $Department->getAllDepartment();

            $this->assign('dadmin',$d);
            $this->assign('departmentList',$departmentList);
            $this->display();

        } else {

            $data['id'] = $_REQUEST['id'];
            $data['email'] = $_REQUEST['email'];
            $data['telphone'] = $_REQUEST['telphone'];
            $data['tname'] = $_REQUEST['tname'];
            $data['department_id'] = $_REQUEST['department_id'];
            $data['password'] = md5($_REQUEST['password']);

            if ($data['password'] === md5('default')) unset($data['password']);

            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

            $Dadmin = D ('Dadmin');
            $flag = $Dadmin->editDadmin($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__APP__.'/Dadmin');
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        }

    }


    public function del() {
        if (!IS_POST) $this->error('非法访问');

        $id = $_REQUEST['id'];
        if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        $Dadmin = D ('Dadmin');
        $flag = $Dadmin->delDadmin($id);
        if ($flag === '000000')
            $this->out('RESULT_OK');
        $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
    }

}